package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.modular.order.model.params.OutOrderParam;
import cn.stylefeng.guns.modular.order.model.result.OutOrderResult;
import cn.stylefeng.guns.modular.order.service.OutOrderService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.order.entity.OutOrderDetailed;
import cn.stylefeng.guns.modular.order.model.params.OutOrderDetailedParam;
import cn.stylefeng.guns.modular.order.service.OutOrderDetailedService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

import java.util.List;


/**
 * 出库订单明细表控制器
 */
@Controller
@RequestMapping("/outOrderDetailed")
public class OutOrderDetailedController extends BaseController {

    private String PREFIX = "/order/outOrderDetailed";

    @Autowired
    private OutOrderDetailedService outOrderDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    @Autowired
    private OutOrderService outOrderService;
    
    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/outOrderDetailed.html";
    }

    /**
     * 订单明细页面
     */
    @RequestMapping("/toQuery")
    public String toQuery( @RequestParam("detailedOutNumber") String detailedOutNumber, Model model) {
        model.addAttribute("detailedOutNumber",detailedOutNumber);
        return PREFIX + "/queryDetailed.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(@RequestParam("outCustomerId") Long outCustomerId, @RequestParam("detailedOutNumber") String detailedOutNumber, Model model) {
        model.addAttribute("outCustomerId",outCustomerId);
        model.addAttribute("detailedOutNumber",detailedOutNumber);
        return PREFIX + "/outOrderDetailed_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("outCustomerId") Long outCustomerId, @RequestParam("detailedOutNumber") String detailedOutNumber, Model model) {
        model.addAttribute("outCustomerId",outCustomerId);
        model.addAttribute("detailedOutNumber",detailedOutNumber);
        return PREFIX + "/outOrderDetailed_edit.html";
    }

    @RequestMapping("/moveOpen")
    public String moveOpen(Model model) {
        OutOrderParam outOrderParam = new OutOrderParam();
        User user = userService.getcurrentUser();
        outOrderParam.setDeliverId(user.getDeliverId());
        outOrderParam.setDeptId(user.getDeptId());
        List<OutOrderResult> outOrderResults = outOrderService.dtjOrderNumberList(outOrderParam, user.getOperateId());
        model.addAttribute("outOrderResults",outOrderResults);
        return PREFIX + "/moveOpen.html";
    }

    /**
     * 移动订单
     * @param outOrderDetailedParam
     * @return
     */
    @RequestMapping("/moveOpenItem")
    @ResponseBody
    public ResponseData moveOpenItem(OutOrderDetailedParam outOrderDetailedParam) {
        if(ToolUtil.isEmpty(outOrderDetailedParam.getId())){
            return ResponseData.error("ID does not exist, cannot operate!");
        }
        if(ToolUtil.isEmpty(outOrderDetailedParam.getDetailedOutNumber())){
            return ResponseData.error("Order number does not exist!");
        }
        this.outOrderDetailedService.update(outOrderDetailedParam);
        return ResponseData.success();
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OutOrderDetailedParam outOrderDetailedParam) {
    	String goodsname = outOrderDetailedParam.getDetailedGoodsname();
    	//解决参数中的 英文括号
    	if (goodsname!=null&&goodsname!="") {
    		goodsname = goodsname.replace("& #40;", "(");
    		goodsname = goodsname.replace("& #41;", ")");
    		outOrderDetailedParam.setDetailedGoodsname(goodsname);
    	}
    	String goodsNo = outOrderDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		outOrderDetailedParam.setGoodsNo(goodsNo);
    	}
        this.outOrderDetailedService.add(outOrderDetailedParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OutOrderDetailedParam outOrderDetailedParam) {
    	String goodsname = outOrderDetailedParam.getDetailedGoodsname();
    	//解决参数中的 英文括号
    	if (goodsname!=null&&goodsname!="") {
    		goodsname = goodsname.replace("& #40;", "(");
    		goodsname = goodsname.replace("& #41;", ")");
    		outOrderDetailedParam.setDetailedGoodsname(goodsname);
    	}
    	String goodsNo = outOrderDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		outOrderDetailedParam.setGoodsNo(goodsNo);
    	}
        this.outOrderDetailedService.update(outOrderDetailedParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OutOrderDetailedParam outOrderDetailedParam) {
        this.outOrderDetailedService.delete(outOrderDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OutOrderDetailedParam outOrderDetailedParam) {
        OutOrderDetailed detail = this.outOrderDetailedService.getById(outOrderDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OutOrderDetailedParam outOrderDetailedParam,String condition) {
        return this.outOrderDetailedService.findPageBySpec(outOrderDetailedParam,condition);
    }

    /**
     * 选择商品
     * @return
     */
    @RequestMapping("/toChooseGoods")
    public String toChooseGoods(){
        return PREFIX + "/toChooseGoods.html";
    }

    /**
     * 查询商品列表
     */
    @ResponseBody
    @RequestMapping("/ChooseGoods")
    public LayuiPageInfo ChooseGoods(BasicGoodsParam basicGoodsParam ,String condition) {
        return this.basicGoodsService.findPageBySpec(basicGoodsParam,condition,0l,0l);
    }

}


